package com.lalilu.lmedia.entity

data class LArtist(
    override val name: String,
    override val songs: List<LSong>
) : MusicParent() {
    override val id: String = name
}

/**
 * 根据特殊的符号将[LArtist.name]拆分成多个[LArtist]
 */
fun List<LArtist>.separate(): List<LArtist> = flatMap {
    it.name.split('/', ';', '、', ',', '，').map { name ->
        LArtist(
            name = name.trim(),
            songs = it.songs
        )
    }
}

/**
 * 根据[LArtist.name]将多个重名的[LArtist]合并
 */
fun List<LArtist>.merge(): List<LArtist> = groupBy { it.name }.map { entry ->
    LArtist(
        name = entry.key,
        songs = entry.value.flatMap { it.songs }
    )
}

/**
 * 将Artist和Song链接起来，使得可以从Song访问到从属于的Artist
 */
fun List<LArtist>.link(): List<LArtist> = onEach { artist ->
    artist.songs.forEach { it.link(artist) }
}